'use strict';
const mongoose = require('mongoose');
const config = require('../config/config.unittest.js')({});
const fs = require('fs');
const readline = require('readline');

let ParserBetter = require('../app/utils/rss/parser');
let parserBetter = new ParserBetter({timeout: 10000});

const UserModel = require('../app/model/User')({
  mongoose,
});
const TopicModel = require('../app/model/Topic')({
  mongoose,
});

mongoose.connect(config.mongoose.url, config.mongoose.options, function (err) {
  if (err) {
    console.error('connect to %s error: ', config.mongoose, err.message);
    process.exit(1);
  }
});

async function main() {
  const users = await UserModel.find({loginname: /中国歌曲排行榜/});
  let cnt = users.length;
  for (let i = 0; i < cnt; i++) {
    const user = users[i];
    let author_name = user.loginname;
    let boards = [...user.default_boards];
    let tags = [...user.default_boards];
    try {
      await TopicModel.updateMany({author_name}, {
        $set: {boards, tags}
      });
    } catch (e) {
      console.error(user.loginname, user.rss, '处理异常');
    } finally {
      console.log(i, cnt, user.loginname);
    }
  }


  process.exit(0);
}

main();


